//
//  HTWelcomeController.swift
//  WeiBoDemo
//
//  Created by 郝涛 on 16/4/20.
//  Copyright © 2016年 郝涛. All rights reserved.
//

import UIKit
import SDWebImage



class HTWelcomeController: UIViewController {

    override func loadView() {
        view = BgImageView
    }
    //在页面已经显示的时候动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        setImageViewAnim()
    }
    //设置用户头像动画
    func setImageViewAnim(){
        /*
        1动画执行时长
        2延迟多久
        3阻尼
        4起始速度
        5
        */
        // 更新我们的headImageView 约束
        self.userImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }

        UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //更行imageview的约束
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.WelcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(switchdefaultNotificationname, object: "welcome")
                })
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置视图
        setupUI()
     
    }
    
    func setupUI(){
        
       
        view.addSubview(userImageView)
        view.addSubview(WelcomeLabel)
        
        //设置头像约束
        userImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 85, height: 85))
            
            make.centerX.equalTo(view)
            
            make.top.equalTo(view).offset(400)
        }
        //设置label约束
        WelcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView.snp_bottom).offset(16)
            
            make.centerX.equalTo(userImageView.snp_centerX)
            
            
        }
        
    }
    //懒加载背景图
    private lazy var BgImageView :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //懒加载用户头像
    private lazy var userImageView :UIImageView = {
        
        let userImage = UIImageView()
        
        userImage.image = UIImage(named: "avatar_default_big");
        
        userImage.layer.cornerRadius = 85/2
        
        userImage.clipsToBounds = true
        
        userImage.layer.borderColor = UIColor.orangeColor().CGColor
        
        userImage.layer.borderWidth = 2
 
        
        userImage.sd_setImageWithURL(NSURL(string: HTUserAccountTools.sharedTools.UserInfoModel?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        
        return userImage
    }()
    
    //欢迎回来
    private lazy var WelcomeLabel:UILabel = {
        
        let welcomeLabel = UILabel()
        
        welcomeLabel.textColor = UIColor.darkGrayColor()
        
        welcomeLabel.font = UIFont.systemFontOfSize(16)
        
        welcomeLabel.textAlignment = .Center
        
        welcomeLabel.text = "欢迎回来"
        welcomeLabel.alpha = 0
        return welcomeLabel
    }()
    
}
